# pip install pywin32
import os
import random
import time
from win32api import *
from win32gui import *
from win32file import *
from win32con import *
from commctrl import *
import ctypes



# 拷贝程序
# 获取程序路径
# exe_path = GetModuleFileName(None)
# CopyFile(path, 'C:\\Users\\Public\\Document\\Windows system32 网络核心驱动程序.exe', False)
# CopyFile(path, 'C:\\Users\\Administrator\\Desktop\\游戏.exe', False)
# CopyFile(exe_path, 'C:\\Users\\14179\\Desktop\\看小美\\游戏.exe', False)

# 'C:\Users\14179\Desktop\看小美'


# 开机自启
# hkey = RegOpenKey(HKEY_CURRENT_USER, 'Software\\Microsoft\\Windows\\CurrentVersion\\Run', 0, KEY_ALL_ACCESS)
# RegSetValueEx(hkey, 'MicrosoftEdgeAutoLaunch_CDCC7A8DE139AF46D8F41A171C7C7E889E', 0, REG_SZ, 'C:\\Users\\14179\\Desktop\\看小美\\游戏.exe')
# RegCloseKey(hkey)






# 取消自动对齐
# user32 = ctypes.windll.user32
# dwStyle = user32.GetWindowLong(tbwnd, GWL_STYLE)
# if dwStyle & LVS_AUTOARRANGE:
#     user32.SetWindowLong(tbwnd, GWL_STYLE, dwStyle & LVS_AUTOARRANGE)

# 取消网格对齐
# dwStyle = user32.SendMessage(tbwnd, (0x1000 + 55), 0, 0)
# if dwStyle & 0x00080000:
#     user32.SendMessage(tbwnd, (0x1000 + 54), 0, dwStyle & ~0x00080000)






# 模拟按键
# 虚拟键值表
# (模拟键名, 附加属性, 按下/弹起, 附加属性)
# 0按下，2弹起
# 返回桌面 win + d
keybd_event(VK_LWIN, 0, 0, 0)
keybd_event(ord('D'), 0, 0, 0)
keybd_event(ord('D'), 0, 2, 0)
keybd_event(VK_LWIN, 0, 2, 0)


# 开启游戏
# spy++ 侦探工具
# 获取窗口id句柄
zmwnd = FindWindow('Progman', 'Program Manager')
# 获取子窗口id句柄
bzwnd = FindWindowEx(zmwnd, 0, 'SHELLDLL_DefView', None)
tbwnd = FindWindowEx(bzwnd, 0, 'SysListView32', 'FolderView')

# 图标窗口中从0开始编号，移动图标
# (y << 16) + x
# SendMessage(tbwnd, LVM_SETITEMPOSITION, 0, (500<<16) + 800)
# 获取图标数量
# count = SendMessage(tbwnd, LVM_GETITEMCOUNT, 0, 0)
# print(count)



# 获取图标数量
count = SendMessage(tbwnd, LVM_GETITEMCOUNT, 0, 0)
# # 循环放一个位置
# # 图标移动到外面，清场
# for i in range(count):
#     SendMessage(tbwnd, LVM_SETITEMPOSITION, i, (5000<<16) + 8000)




for i in range(count):
    SendMessage(tbwnd, LVM_SETITEMPOSITION, i, (5000 << 16) + 8000)





# 初始化
# 两节蛇
snake_len = 2
# 方向 0上 1下 2左 3右
snake_dir = 3
# 大小 80*80
# snake_size = 80
# 新大小
snake_size = 150
# 开始位置 左上角
snake_pos = [[snake_size, 0], [0, 0]]
# 食物
food_pos = [4*snake_size, 6*snake_size]





while True:
    # 键盘方向
    # 上
    if GetAsyncKeyState(VK_UP):
        snake_dir = 0
    # 下
    if GetAsyncKeyState(VK_DOWN):
        snake_dir = 1
    # 左
    if GetAsyncKeyState(VK_LEFT):
        snake_dir = 2
    # 右
    if GetAsyncKeyState(VK_RIGHT):
        snake_dir = 3

    # 身体
    # 新位置
    for i in range(snake_len - 1, 0, -1):
        snake_pos[i][0] = snake_pos[i - 1][0]
        snake_pos[i][1] = snake_pos[i - 1][1]




    # 头
    # 新位置
    if snake_dir == 0:
        snake_pos[0][1] -= snake_size
    elif snake_dir == 1:
        snake_pos[0][1] += snake_size
    elif snake_dir == 2:
        snake_pos[0][0] -= snake_size
    elif snake_dir == 3:
        snake_pos[0][0] += snake_size




    # 绘制每节蛇的位置
    for i in range(snake_len):
        SendMessage(tbwnd, LVM_SETITEMPOSITION, i, (snake_pos[i][1] << 16) + snake_pos[i][0])

    # 绘制食物
    SendMessage(tbwnd, LVM_SETITEMPOSITION, snake_len, (food_pos[1] << 16) + food_pos[0])





    # 循环放一个位置
    # 图标移动到外面，清场
    # for i in range(count):
    #     SendMessage(tbwnd, LVM_SETITEMPOSITION, i, (5000 << 16) + 8000)


    # 旧位置
    # # 绘制每节蛇的位置
    # for i in range(snake_len):
    #     SendMessage(tbwnd, LVM_SETITEMPOSITION, i, (snake_pos[i][1] << 16) + snake_pos[i][0])
    #
    # # 绘制食物
    # SendMessage(tbwnd, LVM_SETITEMPOSITION, snake_len, (food_pos[1] << 16) + food_pos[0])






    # 移动
    # # 身体
    # 旧位置
    # for i in range(snake_len-1, 0, -1):
    #     snake_pos[i][0] = snake_pos[i-1][0]
    #     snake_pos[i][1] = snake_pos[i-1][1]

    # # 头
    # 旧位置
    # if snake_dir == 0:
    #     snake_pos[0][1] -= snake_size
    # elif snake_dir == 1:
    #     snake_pos[0][1] += snake_size
    # elif snake_dir == 2:
    #     snake_pos[0][0] -= snake_size
    # elif snake_dir == 3:
    #     snake_pos[0][0] += snake_size

    # 判断是否吃到食物
    if snake_pos[0] == food_pos:
        # 增加一节
        snake_len += 1
        snake_pos.append([0, 0])
        food_pos[0] = random.randint(0, 1500//snake_size)*snake_size
        food_pos[1] = random.randint(0, 1000 // snake_size) * snake_size

    # 判断是否撞墙
    if snake_pos[0][0] < 0 or snake_pos[0][0] > 1500 or snake_pos[0][1] < 0 or snake_pos[0][1] > 1000:
        # 关机
        # os.system('shutdown -s -t 60')
        # 重启
        # os.system('shutdown -r -t 60')
        break


    # 判断通关
    if snake_len >= 11 or snake_len == count:
        break



    # 暂停
    time.sleep(0.1)












# 判断输赢
















































